﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MediaAutomatorLib.Configurations;

namespace MediaAutomatorLib.Helpers
{
    public class Logger
    {
        private const string LogsFolder = "logs";

        private string logFilePath;
        private StringBuilder _logs = new StringBuilder();

        public Logger(string logsFolderPath)
        {
            string filename = string.Format("scan_{0:dd-MM-yyyy_HH-mm-ss}.log", DateTime.Now);

            
            
            if (!Directory.Exists(logsFolderPath))
                Directory.CreateDirectory(logsFolderPath);

            logFilePath = Path.Combine(logsFolderPath, filename);
            
        }

        public void Log(string message, params object []args)
        {
            _logs.AppendLine(String.Format(message, args));
            Console.WriteLine(message, args);
        }

        public void LogBreak()
        {
            Log("");
        }

        public void Log(Configuration config)
        {
            Log("##########################################################");
            Log("#### CONFIGURATIONS>>>");
            Log("#### {0}: {1}", "Downloads to scan", config.Downloads);
            Log("#### {0}: {1}", "Movies library", config.MoviesLibrary);
            Log("#### {0}: {1}", "TV Shows library", config.TvShowsLibrary);
            Log("##########################################################");
            LogBreak();
        }

        public void Flush()
        {
            TextWriter writer;
            if (File.Exists(logFilePath))
                writer = new StreamWriter(logFilePath);
            else
                writer = File.CreateText(logFilePath);
            writer.Write(_logs);
            writer.Close();
            
        }
    }
}
